home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2004 March / PCWMAR04.iso / Software / Trial / TestTrack Pro 6 / ttprowininstall.exe / %LSMAINDIR% / LicenseServer Help / wwhelp / wwhimpl / common / scripts / highlt.js < prev    next >
Encoding:
JavaScript  |  2003-12-12  |  5.5 KB  |  179 lines

  1. // Copyright (c) 2000-2001 Quadralay Corporation.  All rights reserved.
  2. //
  3.  
  4. function  WWHHighlightWords_Object()
  5. {
  6.   this.mWords = null;
  7.  
  8.   this.fSetWordList = WWHHighlightWords_SetWordList;
  9.   this.fExec        = WWHHighlightWords_Exec;
  10. }
  11.  
  12. function  WWHHighlightWords_SetWordList(ParamWords)
  13. {
  14.   if (WWHFrame.WWHHelp.mSettings.mbHighlightingEnabled)
  15.   {
  16.     this.mWords = ParamWords;
  17.   }
  18.   else
  19.   {
  20.     this.mWords = null;
  21.   }
  22. }
  23.  
  24. function  WWHHighlightWords_Exec()
  25. {
  26.   var  WordList;
  27.   var  MaxIndex;
  28.   var  Index;
  29.   var  WordExpressions;
  30.   var  LongestWordExpression;
  31.   var  MaxExpressionIndex;
  32.   var  ExpressionIndex;
  33.   var  ExpressionHash = null;
  34.   var  ExpressionEntry;
  35.   var  Expression;
  36.   var  LongestWordExpressionKey;
  37.   var  NewHighlightedWords = null;
  38.   var  HighlightedWords = null;
  39.   var  TextRange;
  40.   var  LastCharTextRange;
  41.   var  bMatchFound;
  42.   var  bFirstMatch;
  43.   var  NewHighlightedWordsKey;
  44.  
  45.  
  46.   if (this.mWords != null)
  47.   {
  48.     // Only works under IE on Windows
  49.     //
  50.     if ((WWHFrame.WWHBrowserInfo.mBrowser == 2) &&  // Shorthand for IE
  51.         (WWHFrame.WWHBrowserInfo.mPlatform == 1))   // Shorthand for Windows
  52.     {
  53.       ExpressionHash   = new WWHHighlightWords_ExpressionHash_Object();
  54.       HighlightedWords = new WWHHighlightWords_HighlightedWords_Object();
  55.       bFirstMatch = true;
  56.  
  57.       // Access search words
  58.       //
  59.       WordList = this.mWords.split(" ");
  60.       for (MaxIndex = WordList.length, Index = 0 ; Index < MaxIndex ; Index++)
  61.       {
  62.         if (WordList[Index].length > 0)
  63.         {
  64.           // Determine longest sub-expression between '*'
  65.           //
  66.           WordExpressions = WordList[Index].split("*");
  67.           LongestWordExpression = "";
  68.           for (MaxExpressionIndex = WordExpressions.length, ExpressionIndex = 0 ; ExpressionIndex < MaxExpressionIndex ; ExpressionIndex++)
  69.           {
  70.             if (WordExpressions[ExpressionIndex].length > LongestWordExpression.length)
  71.             {
  72.               LongestWordExpression = WordExpressions[ExpressionIndex];
  73.             }
  74.           }
  75.  
  76.           // Store search expression keyed by longest sub-expression
  77.           //
  78.           ExpressionEntry = ExpressionHash[LongestWordExpression + "~"];
  79.           if (typeof ExpressionEntry == "undefined")
  80.           {
  81.             ExpressionEntry = new WWHHighlightWords_ExpressionEntry_Object();
  82.             ExpressionHash[LongestWordExpression + "~"] = ExpressionEntry;
  83.           }
  84.           Expression = WWHStringUtilities_WordToRegExpWithSpacePattern(WordList[Index]);
  85.           ExpressionEntry.mExpressions[ExpressionEntry.mExpressions.length] = new RegExp(Expression, "i");
  86.         }
  87.       }
  88.  
  89.       // Search document based on longest sub-expressions
  90.       //
  91.       for (LongestWordExpressionKey in ExpressionHash)
  92.       {
  93.         LongestWordExpression = LongestWordExpressionKey.substring(0, LongestWordExpressionKey.length - 1);
  94.         NewHighlightedWords = new WWHHighlightWords_HighlightedWords_Object();
  95.  
  96.         TextRange = WWHFrame.WWHContentFrame.WWHDocumentFrame.document.body.createTextRange();
  97.         TextRange.collapse();
  98.         while (TextRange.findText(LongestWordExpression, 1))
  99.         {
  100.           TextRange.expand("word");
  101.           ExpressionEntry = ExpressionHash[LongestWordExpression + "~"];
  102.  
  103.           // Check word against search expression
  104.           //
  105.           bMatchFound = false;
  106.           MaxExpressionIndex = ExpressionEntry.mExpressions.length;
  107.           ExpressionIndex = 0;
  108.           while (( ! bMatchFound) &&
  109.                  (ExpressionIndex < MaxExpressionIndex))
  110.           {
  111.             if (ExpressionEntry.mExpressions[ExpressionIndex].test(TextRange.text))
  112.             {
  113.               // Highlight text if not processed already
  114.               //
  115.               if (typeof HighlightedWords[TextRange.text + "~"] == "undefined")
  116.               {
  117.                 // Record text highlighted for this expression
  118.                 //
  119.                 NewHighlightedWords[TextRange.text + "~"] = true;
  120.  
  121.                 // Try to trim off trailing whitespace or .s
  122.                 //
  123.                 LastCharTextRange = TextRange.duplicate();
  124.                 LastCharTextRange.moveStart("character", TextRange.text.length - 1);
  125.  
  126.                 if ((LastCharTextRange.text == " ") ||
  127.                     (LastCharTextRange.text == ",") ||
  128.                     (LastCharTextRange.text == "."))
  129.                 {
  130.                   TextRange.moveEnd("character", -1);
  131.                 }
  132.  
  133.                 TextRange.pasteHTML("<span style='background: " + WWHFrame.WWHHelp.mSettings.mHighlightingBackgroundColor + " ; color: " + WWHFrame.WWHHelp.mSettings.mHighlightingForegroundColor + "'>" + TextRange.htmlText + "</span>");
  134.  
  135.                 if (bFirstMatch)
  136.                 {
  137.                   TextRange.scrollIntoView();
  138.  
  139.                   bFirstMatch = false;
  140.                 }
  141.  
  142.                 bMatchFound = true;
  143.               }
  144.             }
  145.  
  146.             ExpressionIndex++;
  147.           }
  148.  
  149.           TextRange.collapse(false);
  150.         }
  151.  
  152.         // Add highlighted words to hash
  153.         //
  154.         for (NewHighlightedWordsKey in NewHighlightedWords)
  155.         {
  156.           HighlightedWords[NewHighlightedWordsKey] = true;
  157.         }
  158.       }
  159.     }
  160.   }
  161.  
  162.   // Highlight words only once
  163.   //
  164.   this.mWords = null;
  165. }
  166.  
  167. function  WWHHighlightWords_ExpressionHash_Object()
  168. {
  169. }
  170.  
  171. function  WWHHighlightWords_ExpressionEntry_Object()
  172. {
  173.   this.mExpressions = new Array();
  174. }
  175.  
  176. function  WWHHighlightWords_HighlightedWords_Object()
  177. {
  178. }
  179.